package ru.spb.osll.myfit.server;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static ru.spb.osll.myfit.server.parser.PageUtils.getFullPathToContent;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

import ru.spb.osll.myfit.common.TextParsingTools;

public class ConfiguratorTest {

  private static final String CONFIG_FILE = new String(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
        "<doc>\n"//
      + " <jars name=\"JARS\" path=\"lib\" />\n" //
      + " <fixtures name=\"FIXTURES\" path=\"lib/fixtures\" />\n"//
      + " <root name=\"ROOT\" path=\"tst/testData\" />\n"//
      + "<execresults name=\"EXECRESULTS\" path=\"testExecuteResults\" />\n"//
      + "<suites name=\"SUITES\" path=\"suite\" />\n"//
      + "<suiteexecresults name=\"SUITERESULTS\" path=\"suiteExecuteResults\" />\n"//
      + "<resources name=\"RESOURCES\" path=\"etc\" />\n"//
      + "<logtransfersize value=\"1024\" />"//
      + "<copyDependencies value=\"true\" />"//
      + "<deleteDependencies value=\"false\" />"//
      + "</doc>\n");//

  private Configurator m_configurator;
  
  @Before
  public void setUp() throws Exception {
    DocumentBuilder xml = null;
    xml = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputStream is = new ByteArrayInputStream(CONFIG_FILE.getBytes());
    Document doc = xml.parse(is);
    m_configurator = Configurator.getInstance();
    m_configurator.loadConfigFile(doc);
  }

  @Test
  public void testConfigurator() {
    assertEquals("lib", m_configurator.getDependenciesDir());
    assertEquals("lib/fixtures", m_configurator.getFixtureDir());
    assertEquals("tst/testData", m_configurator.getRootDir());
    assertEquals("testExecuteResults", m_configurator.getTestResultsDirectory());
    assertEquals("suite", m_configurator.getSuitesDir());
    assertEquals("suiteExecuteResults", m_configurator.getSuiteResultsDirectory());
    assertEquals("etc", m_configurator.getResources());
    assertEquals(1024, m_configurator.getLogTransferSize());
    assertTrue(m_configurator.getCopyDependencies());
    assertFalse(m_configurator.getDeleteDependencies());
  }

  @Test
  public void testGetFullPath() {
    HashMap<String, String> paths = new HashMap<String, String>();

    paths.put("", "tst/testData" + File.separator + "content.txt");
    paths.put("testPath", "tst/testData" + File.separator + "testPath" + File.separator
        + "content.txt");
    paths.put("ancestor.path.testPath", "tst/testData" + File.separator + "ancestor"
        + File.separator + "path" + File.separator + "testPath" + File.separator + "content.txt");
    paths.put("12!.11&", "tst/testData" + File.separator + "12!" + File.separator + "11&"
        + File.separator + "content.txt");
    paths.put(null, "tst/testData" + File.separator + "content.txt");

    for (Entry<String, String> entry : paths.entrySet()) {
      String fullPath = getFullPathToContent(m_configurator.getRootDir(), entry.getKey());
      assertEquals(entry.getValue(), fullPath);
    }
  }

  @Test
  public void testGetPathDeclaration() {
    String pathDeclaration = m_configurator.getPathDeclaration();

    pathDeclaration = pathDeclaration.substring(TextParsingTools.PATH.length());
    String[] jars = pathDeclaration.split(";");

    for (String jar : jars) {
      if (jar.equals(TextParsingTools.CR_and_NEL))
        return;

      File f = new File(jar);

      assertTrue(f.exists());
    }
    assertTrue(jars.length > 5);
  }

}
